package zhtt;

import org.hibernate.Session;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

/**
 * Created by zhtt on 2017/4/10.
 */
public class JunitTest {

    private Session session;

    @Before
    public void setUp(){
        session=HbnUtil.getSession();
    }

    @Test
    public void sqlPage(){
        try{
            session.beginTransaction();
            String sql="select * from zhtt0_student limit ?,?";
            List<Student> list=session.createSQLQuery(sql)
                    .addEntity(Student.class)
                    .setParameter(0,0)/** 从第一条开始 **/
                    .setParameter(1,5)/** 需要显示5条 **/
                    .list();
            for(Student student:list){
                System.out.println(student);
            }
            session.getTransaction().commit();
        }catch (Exception e){
            e.printStackTrace();;
        }
    }

    @Test
    public void hqlPage(){
        try{
            session.beginTransaction();
            String hql="from Student";
            List<Student> list=session.createQuery(hql)
                    .setFirstResult(0)
                    .setMaxResults(5)
                    .list();
            for(Student student:list){
                System.out.println(student);
            }
            session.getTransaction().commit();
        }catch (Exception e){
            e.printStackTrace();;
        }
    }

    @Test
    public void qbcPage(){
        try{
            session.beginTransaction();
            List<Student> list=session.createCriteria(Student.class)
                    .setFirstResult(0)
                    .setMaxResults(5)
                    .list();
            for(Student student:list){
                System.out.println(student);
            }
            session.getTransaction().commit();
        }catch (Exception e){
            e.printStackTrace();;
        }
    }
}
